<template>
  <div style="margin-right: 30px">
    <el-form ref="formObj" :model="wyform" :rules="rules" label-width="100px">
      <el-form-item label="权限名称" prop="name">
        <el-input maxLength="20" v-model="wyform.name" placeholder="请输入权限名称"></el-input>
      </el-form-item>
			<el-form-item label="前端地址" prop="frontUrl">
			  <el-input v-model="wyform.frontUrl" placeholder="请输入前端地址"></el-input>
			</el-form-item>
			<el-form-item label="后端地址" prop="backUrl">
			  <el-input v-model="wyform.backUrl" placeholder="请输入后端地址"></el-input>
			</el-form-item>
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="wyform.type">
          <el-radio :value="1">目录</el-radio>
          <el-radio :value="0">按钮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限说明" prop="description">
        <el-input type="textarea" :rows="5" v-model="wyform.description" placeholder="请输入权限说明"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain :icon="Save" @click="save">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import Save from '@/components/icons/save'
import { ref, reactive } from 'vue'
import { get, post } from '@/axios'
import url from './util'
const emits = defineEmits(['update:show', 'getTableData'])
const props = defineProps(['id', 'parentId', 'level'])
const wyform = reactive({
	id: null,
  name: '',
  description: '',
  parentId: null,
  frontUrl: '',
  backUrl: '',
  level: null,
  type: null
})
const rules = reactive({
	/* name: [
		{ required: true, message: '请输入管理员姓名', trigger: 'blur' }
	],
	nickyName: [
		{ required: true, message: '请输入管理员昵称', trigger: 'blur' }
	],
	sex: [
		{ required: true, message: '请选择管理员性别', trigger: 'blur' }
	],
	birthday: [
		{ required: true, message: '请输入管理员生日', trigger: 'blur' }
	],
	phone: [
		{ required: true, message: '请输入手机号码', trigger: 'blur' },
		{ pattern: /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/, message: '请输入正确的中国手机号', trigger: 'blur' },
		{ validator: check, message: '该手机号已经被使用', trigger: 'blur' }
	],
	email: [
		{ required: true, message: '请输入电子信箱', trigger: 'blur' },
		{ type: 'email', message: '请输入正确的电子信箱', trigger: 'blur' },
		{ validator: check, message: '该电子信箱已经被使用', trigger: 'blur' }
	],
	password: [
		{ required: true, message: '请输入登录密码', trigger: 'blur' }
	],
	file: [
		{ required: props.id === null, message: '请上传管理员头像', trigger: 'blur' }
	] */
})
const formObj = ref()
if (props.id) {
	wyform.id = props.id
	getById()
} else {
	wyform.parentId = props.parentId
	wyform.level = props.level
}
function check (rule, value, callback) {
	get(url.check, { id: props.id, value, field: rule.field }, content => {
		if (content) {
			callback()
		} else {
			callback(new Error())
		}
	})
}
function save() {
	post(props.id ? url.update : url.add, wyform, content => {
		emits('update:show', false)
		emits('getTableData')
	}, formObj)
}
function getById () {
	get(url.getById, { id: props.id }, content => {
		for (const key in wyform) {
			if (Object.prototype.hasOwnProperty.call(content, key)) {
				wyform[key] = content[key]
			}
		}
	})
}
</script>

<style scoped lang="scss"></style>
